export default {
  // #ifdef APP-PLUS
  url: uni.getStorageSync('url') || 'http://localhost:8088/',
  // url: 'http://218.7.112.123:10001/',
  // #endif

  // #ifndef APP-PLUS
  url: '/backend/',
  // #endif

  request(options = {}) {
    return new Promise((resolve, reject) => {
      console.log("this.url: ", this.url);
      uni.request({
        url: this.url + options.url,
        method: options.method || 'GET',
        data: options.data,
        header: options.header,
        success: res => {
          return resolve(res.data)
        },
        fail: (err) => {
          uni.showToast({
            title: '服务器开小差了',
            icon: 'none',
            position: 'center'
          });
          uni.hideLoading() // 已经错误了，不管页面上有没有提示框，都要关闭
          return reject(err)
        }
      });
    })
  },
  get(url, header = {}, options = {}) {
    options.url = url
    options.header = header
    options.method = 'GET'
    return this.request(options)
  },
  delete(url, header = {}, options = {}) {
    options.url = url
    options.header = header
    options.method = 'DELETE'
    return this.request(options)
  },
  post(url, data = {}, header = {}, options = {}) {
    options.url = url
    options.data = data
    options.header = header
    options.method = 'POST'
    return this.request(options)
  },
  put(url, data = {}, header = {}, options = {}) {
    options.url = url
    options.data = data
    options.header = header
    options.method = 'PUT'
    return this.request(options)
  }
}
